"""
爬虫：抓取sukebei中fc2的磁力链接，存储到mysql中
"""
import re
import time
import mysql.connector
import common.gyblog as log
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService

LOG = log.get_logger()
SERVICE = ChromeService()
MYSQL_DB = mysql.connector.connect(
    host="192.168.0.180",
    user="root",
    passwd="123456",
    database="movies_record"
)


def get_page_info(keyword, i):
    try:
        url = 'https://sukebei.nyaa.si/?f=2&c=0_0&q={}&p={}'.format(keyword, str(i))
        # 创建 WebDriver 对象
        driver = webdriver.Chrome(service=SERVICE)
        driver.get(url)
        names = driver.find_elements(By.XPATH, '/html/body/div/div[2]/table/tbody/tr[position()]/td[2]/a')
        tors = driver.find_elements(By.XPATH, '/html/body/div/div[2]/table/tbody/tr[position()]/td[3]/a[2]')
        current_date = time.strftime("%Y-%m-%d", time.localtime())
        print('URL:{},共抓取到{}条信息'.format(url, len(tors)))
        LOG.info('URL:{},共抓取到{}条信息'.format(url, len(tors)))
        for i in range(0, len(tors)):
            name = str(names[i].get_attribute('title'))
            tor_text = str(tors[i].get_attribute('href'))
            nums = re.findall("FC2-PPV-\d+", name)
            if len(nums) == 1:
                num = nums[0]
            else:
                num = '未知番号'
            tor = re.findall("magnet:\?xt=urn:.*?&dn", tor_text)[0]
            info_dict = (num, name, tor, current_date, 'N', 'N', 'N', 'N')
            check_and_save(info_dict)
        driver.close()
        return 1
    except:
        print('抓取失败')
        return 0


def get_info(keyword, page):
    for i in range(1, page):
        while True:
            ans = get_page_info(keyword, i)
            if ans == 1:
                break


# info = (num,name,tor,save_time,is_download,is_has_been_exist,is_exist,is_like)
def insert_fc2(info):
    db_cursor = MYSQL_DB.cursor()
    sql = ("INSERT INTO fc2_record (num,name,tor,save_time,is_download,is_has_been_exist,is_exist,is_like) VALUES ("
           "%s, %s, %s, %s, %s, %s, %s, %s)")
    db_cursor.execute(sql, info)
    MYSQL_DB.commit()


# 查询是否在数据库中(tor和num都不存在，才新增记录)，没有则插入
def check_and_save(info_dict):
    db_cursor = MYSQL_DB.cursor()
    sql_tor = "SELECT * FROM fc2_record WHERE tor='{}'".format(info_dict[2])
    db_cursor.execute(sql_tor)
    results_tor = db_cursor.fetchall()
    if len(results_tor) == 0:
        sql_name = "SELECT * FROM fc2_record WHERE num='{}'".format(info_dict[0])
        db_cursor.execute(sql_name)
        results_name = db_cursor.fetchall()
        if len(results_name) == 0:
            print('不存在', info_dict)
            insert_fc2(info_dict)
        else:
            print('存在,name冲突', info_dict)
    else:
        print('存在,tor冲突', info_dict)


if __name__ == '__main__':
    keywords = ['FC2']
    page = 5  # 取值范围2-15
    for keyword in keywords:
        get_info(keyword, page)
